package cn.itcast.bos.service.take_delivery.Impl;

import cn.itcast.bos.Dao.take_delicery.WayBillDao;
import cn.itcast.bos.domain.take_delivery.WayBill;
import cn.itcast.bos.index.WayBillIndexRepository;
import cn.itcast.bos.service.take_delivery.WayBillService;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.index.query.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.InvocationTargetException;

@Service
@Transactional
public class WayBillServiceImpl implements WayBillService {
    @Autowired
    private WayBillDao wayBillDao;

    @Autowired
    private WayBillIndexRepository wayBillIndexRepository;
    @Override
    public void save(WayBill wayBill) {
        //查找要保存的waybill是否已经存在
        WayBill presistWayBill = wayBillDao.findByWayBillNum(wayBill.getWayBillNum());
        if(presistWayBill ==null||presistWayBill.getId()==null){
            //运单不存在
            wayBill.setSignStatus(1);
            wayBillDao.save(wayBill);
            //保存到elasticsearch中
            wayBillIndexRepository.save(wayBill);
        }else{
            Integer id = presistWayBill.getId();
            try {
                BeanUtils.copyProperties(presistWayBill,wayBill);
                presistWayBill.setId(id);
                wayBillDao.save(presistWayBill);
                //保存到elasticsearch中
                wayBillIndexRepository.save(presistWayBill);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    //条件查询的方法
    public Page<WayBill> findAll(WayBill wayBill, Pageable pageable) {
        //先判断是否有条件
        if(StringUtils.isBlank(wayBill.getWayBillNum())
                &&StringUtils.isBlank(wayBill.getSendAddress())
                &&StringUtils.isBlank(wayBill.getRecAddress())
                &&StringUtils.isBlank(wayBill.getSendProNum())
                &&(wayBill.getSignStatus()==null||wayBill.getSignStatus()==0)){
                                     return wayBillDao.findAll(pageable);
        }else {
            //根据条件查询
            BoolQueryBuilder query = new BoolQueryBuilder();
            if(StringUtils.isNoneBlank(wayBill.getWayBillNum())){
                //运单号查询
                QueryBuilder temquery = new TermQueryBuilder("wayBillNum",wayBill.getWayBillNum());
                query.must(temquery);
            }
            if(StringUtils.isNoneBlank(wayBill.getSendAddress())){
                //发送地址模糊查询
                //情况一:输入"北"是词条的一部分,使用模糊查询匹配
                QueryBuilder wildcordQuery = new WildcardQueryBuilder("sendAddress","*"+wayBill.getSendAddress()+"*");
                //情况二:输入"北京市海淀区"是多个词条的组合,
                QueryBuilder queryStringQueryBuilder = new QueryStringQueryBuilder(wayBill.getSendAddress()).field("sendAddress").defaultOperator(QueryStringQueryBuilder.Operator.AND);
               BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
               //情况一与情况二是or的关系
               boolQueryBuilder.should(wildcordQuery);
               boolQueryBuilder.should(queryStringQueryBuilder);
               //同时boolQueryBuilder与其他条件是and 的关系
                query.must(boolQueryBuilder);
            }
            if(StringUtils.isNoneBlank(wayBill.getRecAddress())){
                QueryBuilder wildcordQuery=new WildcardQueryBuilder("recAddress","*"+wayBill.getRecAddress()+"*");
                QueryBuilder queryStringQueryBuilder = new QueryStringQueryBuilder(wayBill.getRecAddress()).field("recAddress").defaultOperator(QueryStringQueryBuilder.Operator.AND);
                BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
                //情况一与情况二是or的关系
                boolQueryBuilder.should(wildcordQuery);
                boolQueryBuilder.should(queryStringQueryBuilder);
                //同时boolQueryBuilder与其他条件是and 的关系
                query.must(boolQueryBuilder);
            }
            if(StringUtils.isNoneBlank(wayBill.getSendProNum())){
                QueryBuilder temQuery = new TermQueryBuilder("sendProNum",wayBill.getSendProNum());
                query.must(temQuery);
            }
            if(wayBill.getSignStatus()!=null &&wayBill.getSignStatus()!=0){
                QueryBuilder temQuery= new TermQueryBuilder("signStatus",wayBill.getSignStatus());
                query.must(temQuery);
            }
            SearchQuery searchQuery = new NativeSearchQuery(query);
            searchQuery.setPageable(pageable);//分页效果
            return wayBillIndexRepository.search(searchQuery);

        }

    }

    @Override
    public WayBill findByWayBillNum(String wayBillNum) {
        return wayBillDao.findByWayBillNum(wayBillNum);
    }
}
